﻿using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace SleepMonitor.App.Business
{
	public class FileManager
	{
		public FileManager()
		{
		}

		public File ImportTextFile(string path)
		{
			File file = new File();
			file.Sensors.Add(new Sensor());
			file.Sensors.Add(new Sensor());
			file.Sensors.Add(new Sensor());
			file.Sensors.Add(new Sensor());

			using (TextReader textReader = new StreamReader(path))
			{

				try
				{
					while (textReader.Peek() != -1)
					{
						string line = textReader.ReadLine();
						string[] words = line.Split(' ');
						int year = int.Parse(words[0].Substring(0, 4));
						int month = int.Parse(words[0].Substring(4, 2));
						int day = int.Parse(words[0].Substring(6, 2));
						int hour = int.Parse(words[1].Substring(0, 2));
						int min = int.Parse(words[1].Substring(2, 2));
						int sec = int.Parse(words[1].Substring(4, 2));
						int ms = int.Parse(words[1].Substring(6, 3));
						DateTime currentLine = new DateTime(year, month, day, hour, min, sec, ms);

						for (int i = 2; i < 6; i++)
						{
							Point p = new Point(currentLine, double.Parse(words[i].Substring(0, words[i].Length - 1)));
							file.Sensors[i - 2].Points.Add(p);
						}
					}
				}
				finally
				{
					textReader.Close();
				}
			}
			return file;
		}



		/// <summary>
		/// Saves the specified file.
		/// </summary>
		/// <param name="file">The file object that will be saved.</param>
		/// <param name="path">The path where that file will be saved.</param>
		public void Save(File file, string path)
		{
			XmlSerializer serializer = new XmlSerializer(typeof(File));


			bool exists = System.IO.File.Exists(path);
			string backupFile = Path.ChangeExtension(path, "bak");


			if (exists)
			{
				System.IO.File.Move(path, backupFile);
			}

			try
			{
				using (Stream baseStream = new FileStream(path, FileMode.Create))
				using (Stream zipStream = new GZipStream(baseStream, CompressionMode.Compress))
				using (TextWriter writer = new StreamWriter(zipStream))
				{
					serializer.Serialize(writer, file);
				}
				System.IO.File.Delete(backupFile);
			}
			catch
			{
				System.IO.File.Delete(path);
				System.IO.File.Move(backupFile, path);
				throw;
			}
		}


		/// <summary>
		/// Openss the specified file.
		/// </summary>
		/// <param name="path">The path where the file that will be opened is.</param>
		public File Open(string path)
		{
			File retVal = null;

			XmlSerializer serializer = new XmlSerializer(typeof(File));

			using (Stream baseStream = System.IO.File.OpenRead(path))
			using (Stream zipStream = new GZipStream(baseStream, CompressionMode.Decompress))
			using (TextReader reader = new StreamReader(zipStream))
			{
				retVal = (File)serializer.Deserialize(reader);
			}

			return retVal;
		}
	}
}
